home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tests / regexp.test < prev    next >
Text File  |  1993-06-17  |  11KB  |  299 lines

  1. # Commands covered:  regexp, regsub
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/regexp.test,v 1.10 93/06/17 13:31:55 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. catch {unset foo}
  32. test regexp-1.1 {basic regexp operation} {
  33.     regexp ab*c abbbc
  34. } 1
  35. test regexp-1.2 {basic regexp operation} {
  36.     regexp ab*c ac
  37. } 1
  38. test regexp-1.3 {basic regexp operation} {
  39.     regexp ab*c ab
  40. } 0
  41. test regexp-1.4 {basic regexp operation} {
  42.     regexp -- -gorp abc-gorpxxx
  43. } 1
  44.  
  45. test regexp-2.1 {getting substrings back from regexp} {
  46.     set foo {}
  47.     list [regexp ab*c abbbbc foo] $foo
  48. } {1 abbbbc}
  49. test regexp-2.2 {getting substrings back from regexp} {
  50.     set foo {}
  51.     set f2 {}
  52.     list [regexp a(b*)c abbbbc foo f2] $foo $f2
  53. } {1 abbbbc bbbb}
  54. test regexp-2.3 {getting substrings back from regexp} {
  55.     set foo {}
  56.     set f2 {}
  57.     list [regexp a(b*)(c) abbbbc foo f2] $foo $f2
  58. } {1 abbbbc bbbb}
  59. test regexp-2.4 {getting substrings back from regexp} {
  60.     set foo {}
  61.     set f2 {}
  62.     set f3 {}
  63.     list [regexp a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
  64. } {1 abbbbc bbbb c}
  65. test regexp-2.5 {getting substrings back from regexp} {
  66.     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
  67.     set f6 {}; set f7 {}; set f8 {}; set f9 {}
  68.     list [regexp (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) 12223345556789999 \
  69.         foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
  70.         $f6 $f7 $f8 $f9
  71. } {1 12223345556789999 1 222 33 4 555 6 7 8 9999}
  72. test regexp-2.6 {getting substrings back from regexp} {
  73.     set foo 2; set f2 2; set f3 2; set f4 2
  74.     list [regexp (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
  75. } {1 a a {} {}}
  76. test regexp-2.7 {getting substrings back from regexp} {
  77.     set foo 1; set f2 1; set f3 1; set f4 1
  78.     list [regexp (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
  79. } {1 ac a {} c}
  80.  
  81. test regexp-3.1 {-indices option to regexp} {
  82.     set foo {}
  83.     list [regexp -indices ab*c abbbbc foo] $foo
  84. } {1 {0 5}}
  85. test regexp-3.2 {-indices option to regexp} {
  86.     set foo {}
  87.     set f2 {}
  88.     list [regexp -indices a(b*)c abbbbc foo f2] $foo $f2
  89. } {1 {0 5} {1 4}}
  90. test regexp-3.3 {-indices option to regexp} {
  91.     set foo {}
  92.     set f2 {}
  93.     list [regexp -indices a(b*)(c) abbbbc foo f2] $foo $f2
  94. } {1 {0 5} {1 4}}
  95. test regexp-3.4 {-indices option to regexp} {
  96.     set foo {}
  97.     set f2 {}
  98.     set f3 {}
  99.     list [regexp -indices a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
  100. } {1 {0 5} {1 4} {5 5}}
  101. test regexp-3.5 {-indices option to regexp} {
  102.     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
  103.     set f6 {}; set f7 {}; set f8 {}; set f9 {}
  104.     list [regexp -indices (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) \
  105.         12223345556789999 \
  106.         foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
  107.         $f6 $f7 $f8 $f9
  108. } {1 {0 16} {0 0} {1 3} {4 5} {6 6} {7 9} {10 10} {11 11} {12 12} {13 16}}
  109. test regexp-3.6 {getting substrings back from regexp} {
  110.     set foo 2; set f2 2; set f3 2; set f4 2
  111.     list [regexp -indices (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
  112. } {1 {1 1} {1 1} {-1 -1} {-1 -1}}
  113. test regexp-3.7 {getting substrings back from regexp} {
  114.     set foo 1; set f2 1; set f3 1; set f4 1
  115.     list [regexp -indices (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
  116. } {1 {1 2} {1 1} {-1 -1} {2 2}}
  117.  
  118. test regexp-4.1 {-nocase option to regexp} {
  119.     regexp -nocase foo abcFOo
  120. } 1
  121. test regexp-4.2 {-nocase option to regexp} {
  122.     set f1 22
  123.     set f2 33
  124.     set f3 44
  125.     list [regexp -nocase {a(b*)([xy]*)z} aBbbxYXxxZ22 f1 f2 f3] $f1 $f2 $f3
  126. } {1 aBbbxYXxxZ Bbb xYXxx}
  127.  
  128. test regexp-5.1 {exercise cache of compiled expressions} {
  129.     regexp .*a b
  130.     regexp .*b c
  131.     regexp .*c d
  132.     regexp .*d e
  133.     regexp .*e f
  134.     regexp .*a bbba
  135. } 1
  136. test regexp-5.2 {exercise cache of compiled expressions} {
  137.     regexp .*a b
  138.     regexp .*b c
  139.     regexp .*c d
  140.     regexp .*d e
  141.     regexp .*e f
  142.     regexp .*b xxxb
  143. } 1
  144. test regexp-5.3 {exercise cache of compiled expressions} {
  145.     regexp .*a b
  146.     regexp .*b c
  147.     regexp .*c d
  148.     regexp .*d e
  149.     regexp .*e f
  150.     regexp .*c yyyc
  151. } 1
  152. test regexp-5.4 {exercise cache of compiled expressions} {
  153.     regexp .*a b
  154.     regexp .*b c
  155.     regexp .*c d
  156.     regexp .*d e
  157.     regexp .*e f
  158.     regexp .*d 1d
  159. } 1
  160. test regexp-5.5 {exercise cache of compiled expressions} {
  161.     regexp .*a b
  162.     regexp .*b c
  163.     regexp .*c d
  164.     regexp .*d e
  165.     regexp .*e f
  166.     regexp .*e xe
  167. } 1
  168.  
  169. test regexp-6.1 {regexp errors} {
  170.     list [catch {regexp a} msg] $msg
  171. } {1 {wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}}
  172. test regexp-6.2 {regexp errors} {
  173.     list [catch {regexp -nocase a} msg] $msg
  174. } {1 {wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}}
  175. test regexp-6.3 {regexp errors} {
  176.     list [catch {regexp -gorp a} msg] $msg
  177. } {1 {bad switch "-gorp": must be -indices, -nocase, or --}}
  178. test regexp-6.4 {regexp errors} {
  179.     list [catch {regexp a( b} msg] $msg
  180. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  181. test regexp-6.5 {regexp errors} {
  182.     list [catch {regexp a( b} msg] $msg
  183. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  184. test regexp-6.6 {regexp errors} {
  185.     list [catch {regexp a a f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1} msg] $msg
  186. } {1 {too many substring variables}}
  187. test regexp-6.7 {regexp errors} {
  188.     set f1 44
  189.     list [catch {regexp abc abc f1(f2)} msg] $msg
  190. } {1 {couldn't set variable "f1(f2)"}}
  191.  
  192. test regexp-7.1 {basic regsub operation} {
  193.     list [regsub aa+ xaxaaaxaa 111&222 foo] $foo
  194. } {1 xax111aaa222xaa}
  195. test regexp-7.2 {basic regsub operation} {
  196.     list [regsub aa+ aaaxaa &111 foo] $foo
  197. } {1 aaa111xaa}
  198. test regexp-7.3 {basic regsub operation} {
  199.     list [regsub aa+ xaxaaa 111& foo] $foo
  200. } {1 xax111aaa}
  201. test regexp-7.4 {basic regsub operation} {
  202.     list [regsub aa+ aaa 11&2&333 foo] $foo
  203. } {1 11aaa2aaa333}
  204. test regexp-7.5 {basic regsub operation} {
  205.     list [regsub aa+ xaxaaaxaa &2&333 foo] $foo
  206. } {1 xaxaaa2aaa333xaa}
  207. test regexp-7.6 {basic regsub operation} {
  208.     list [regsub aa+ xaxaaaxaa 1&22& foo] $foo
  209. } {1 xax1aaa22aaaxaa}
  210. test regexp-7.7 {basic regsub operation} {
  211.     list [regsub a(a+) xaxaaaxaa {1\122\1} foo] $foo
  212. } {1 xax1aa22aaxaa}
  213. test regexp-7.8 {basic regsub operation} {
  214.     list [regsub a(a+) xaxaaaxaa {1\\\122\1} foo] $foo
  215. } "1 {xax1\\aa22aaxaa}"
  216. test regexp-7.9 {basic regsub operation} {
  217.     list [regsub a(a+) xaxaaaxaa {1\\122\1} foo] $foo
  218. } "1 {xax1\\122aaxaa}"
  219. test regexp-7.10 {basic regsub operation} {
  220.     list [regsub a(a+) xaxaaaxaa {1\\&\1} foo] $foo
  221. } "1 {xax1\\aaaaaxaa}"
  222. test regexp-7.11 {basic regsub operation} {
  223.     list [regsub a(a+) xaxaaaxaa {1\&\1} foo] $foo
  224. } {1 xax1&aaxaa}
  225. test regexp-7.12 {basic regsub operation} {
  226.     list [regsub a(a+) xaxaaaxaa {\1\1\1\1&&} foo] $foo
  227. } {1 xaxaaaaaaaaaaaaaaxaa}
  228. test regexp-7.13 {basic regsub operation} {
  229.     set foo xxx
  230.     list [regsub abc xyz 111 foo] $foo
  231. } {0 xyz}
  232. test regexp-7.14 {basic regsub operation} {
  233.     set foo xxx
  234.     list [regsub ^ xyz "111 " foo] $foo
  235. } {1 {111 xyz}}
  236. test regexp-7.15 {basic regsub operation} {
  237.     set foo xxx
  238.     list [regsub -- -foo abc-foodef "111 " foo] $foo
  239. } {1 {abc111 def}}
  240.  
  241.  
  242. test regexp-8.1 {case conversion in regsub} {
  243.     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
  244. } {1 xaAAaAAay}
  245. test regexp-8.2 {case conversion in regsub} {
  246.     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
  247. } {1 xaAAaAAay}
  248. test regexp-8.3 {case conversion in regsub} {
  249.     set foo 123
  250.     list [regsub a(a+) xaAAaAAay & foo] $foo
  251. } {0 xaAAaAAay}
  252.  
  253. test regexp-9.1 {-all option to regsub} {
  254.     set foo 86
  255.     list [regsub -all x+ axxxbxxcxdx |&| foo] $foo
  256. } {1 a|xxx|b|xx|c|x|d|x|}
  257. test regexp-9.2 {-all option to regsub} {
  258.     set foo 86
  259.     list [regsub -nocase -all x+ aXxXbxxcXdx |&| foo] $foo
  260. } {1 a|XxX|b|xx|c|X|d|x|}
  261. test regexp-9.3 {-all option to regsub} {
  262.     set foo 86
  263.     list [regsub x+ axxxbxxcxdx |&| foo] $foo
  264. } {1 a|xxx|bxxcxdx}
  265. test regexp-9.4 {-all option to regsub} {
  266.     set foo 86
  267.     list [regsub -all bc axxxbxxcxdx |&| foo] $foo
  268. } {0 axxxbxxcxdx}
  269. test regexp-9.5 {-all option to regsub} {
  270.     set foo xxx
  271.     list [regsub -all node "node node more" yy foo] $foo
  272. } {1 {yy yy more}}
  273. test regexp-9.6 {-all option to regsub} {
  274.     set foo xxx
  275.     list [regsub -all ^ xxx 123 foo] $foo
  276. } {1 123xxx}
  277.  
  278. test regexp-10.1 {regsub errors} {
  279.     list [catch {regsub a b c} msg] $msg
  280. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  281. test regexp-10.2 {regsub errors} {
  282.     list [catch {regsub -nocase a b c} msg] $msg
  283. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  284. test regexp-10.3 {regsub errors} {
  285.     list [catch {regsub -nocase -all a b c} msg] $msg
  286. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  287. test regexp-10.4 {regsub errors} {
  288.     list [catch {regsub a b c d e f} msg] $msg
  289. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  290. test regexp-10.5 {regsub errors} {
  291.     list [catch {regsub -gorp a b c} msg] $msg
  292. } {1 {bad switch "-gorp": must be -all, -nocase, or --}}
  293. test regexp-10.6 {regsub errors} {
  294.     list [catch {regsub -nocase a( b c d} msg] $msg
  295. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  296. test regexp-10.7 {regsub errors} {
  297.     list [catch {regsub -nocase aaa aaa xxx f1(f2)} msg] $msg
  298. } {1 {couldn't set variable "f1(f2)"}}
  299.